import Cookie from 'js-cookie'
import Qs from 'qs'
import * as CollectionRequest from "~/assets/collection/collection";
import * as CartRequest from "~/assets/cart/cart";
import * as AccountRequest from "~/assets/accountInfo/accountInfo";
export const getJWTToken = (vm, form) => {
  let data = {
    password: form.password,
    rememberMe: true,
    username: form.username
  }
  vm.$axios.post('/auth/authenticate', data)
    .then((res) => {
      Cookie.set("practiceJwtToken", res.data.id_token);
      getUserInfo(vm,form)
    })
    .catch(function () {
      vm.$message.error("账号或密码错误")
    })
}

export const getUserInfo = (vm, form) => {
  let data = Qs.stringify({
    username: form.username
  });
   vm.$axios.post('/suser/select', data)
    .then((res) => {
      vm.$store.state.userInfo = res.data.data
      vm.$store.state.loginFlag = true
      CollectionRequest.getCollectionInfo(vm,res.data.data.id);
      CartRequest.getCartInfo(vm);
      vm.$router.push("/")
    })
}

export const register = (vm) => {
  let data = {
    avatar:'',
    createTime: '',
    isActive:'',
    updateTime:'',
    email:vm.ruleForm.email,
    password: vm.ruleForm.enterPassword,
    phone: vm.ruleForm.telephone,
    username: vm.ruleForm.name
  }
  vm.$axios.post('/user/register', data)
    .then((res) => {
      console.log(res)
      vm.$router.push('/login/login')
    })
}


